var t = getApp(), a = require("../../../component/WxNotificationCenter/WxNotificationCenter.js");

Page({
    data: {
        topCarH: 0,
        tabIndex: 0,
        weddingOrderId: "",
        detail: {},
        pageNo: 1,
        newUserPageNo: 1,
        newUserTotal: -1,
        collectUserTotal: -1,
        newUserData: [],
        userData: [],
        isLoading: !1
    },
    clickCollectTab: function(t) {
        1 != this.data.tabIndex && (this.setData({
            tabIndex: 1,
            userData: [],
            collectUserTotal: -1
        }), this.loadCollectUserData(!0));
    },
    clickNew: function(t) {
        0 != this.data.tabIndex && this.setData({
            tabIndex: 0,
            userData: this.data.newUserData
        });
    },
    clickWork: function(a) {
        var e = a.currentTarget.dataset.accesstoken;
        t.util.navToUserHome(e);
    },
    clickPhone: function(a) {
        var e = a.currentTarget.dataset.accesstoken;
        t.util.callPhone(e);
    },
    clickSelect: function(t) {
        var a = t.currentTarget.dataset.orderid, e = this;
        wx.showModal({
            title: "提示",
            content: "确定选中该用户？",
            success: function(t) {
                t.confirm && e.selectUser(a);
            }
        });
    },
    clickStop: function(t) {
        var a = this;
        wx.showModal({
            title: "提示",
            content: "确定停止档期查询？",
            success: function(t) {
                t.confirm && a.stopSearch();
            }
        });
    },
    clickDel: function(t) {
        var a = t.currentTarget.dataset.userid, e = t.currentTarget.dataset.index, i = this;
        wx.showModal({
            title: "提示",
            content: "确定删除该用户抢单",
            success: function(t) {
                t.confirm && i.delUser(e, a);
            }
        });
    },
    clickCollect: function(t) {
        var a = t.currentTarget.dataset.userid, e = t.currentTarget.dataset.index, i = t.currentTarget.dataset.collect;
        this.collectUser(e, a, parseInt(i));
    },
    clickCooperate: function(t) {
        var a = t.currentTarget.dataset.userid, e = t.currentTarget.dataset.index, i = t.currentTarget.dataset.collect;
        this.cooperateUser(e, a, parseInt(i));
    },
    clickShare: function(t) {},
    clickOrderDel: function(t) {
        var a = this;
        wx.showModal({
            title: "提示",
            content: "删除后无法再查看抢单用户结果，确定删除?",
            success: function(t) {
                t.confirm && a.delOrder();
            }
        });
    },
    loadData: function() {
        var a = this;
        t.network.post({
            url: t.url.WeddingOrderDetail,
            params: {
                weddingOrderId: a.data.weddingOrderId
            },
            before: function() {
                wx.showNavigationBarLoading();
            },
            success: function(e) {
                1 == e.data.result ? (a.setData({
                    detail: e.data.data
                }), wx.createSelectorQuery().select(".card_top_content").boundingClientRect(function(t) {
                    a.setData({
                        topCarH: 2 * t.height + 30
                    });
                }).exec(), a.loadUserData(!0)) : t.util.showToast(e.data.message);
            },
            fail: function() {
                t.util.showToast("获取数据失败");
            },
            complete: function() {
                wx.hideNavigationBarLoading();
            }
        });
    },
    stopSearch: function() {
        var a = this;
        t.network.post({
            url: t.url.StopWeddingOrder,
            params: {
                weddingOrderId: a.data.weddingOrderId
            },
            before: function() {
                wx.showLoading({
                    title: "正在停止"
                });
            },
            success: function(e) {
                1 == e.data.result ? a.updateLocalDataStop() : t.util.showToast(e.data.message);
            },
            fail: function() {
                t.util.showToast("停止失败");
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
    selectUser: function(a) {
        var e = this;
        t.network.post({
            url: t.url.SelectWeddingOrderUser,
            params: {
                weddingOrderUserId: a
            },
            before: function() {
                wx.showLoading({
                    title: "正在提交"
                });
            },
            success: function(i) {
                1 == i.data.result ? e.updateLocalDataSeletUser(a) : t.util.showToast(i.data.message);
            },
            fail: function() {
                t.util.showToast("获取数据失败");
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
    delOrder: function(e) {
        var i = this;
        t.network.post({
            url: t.url.DeleteWeddingOrder,
            params: {
                weddingOrderId: i.data.weddingOrderId
            },
            before: function() {
                wx.showLoading({
                    title: "正在删除"
                });
            },
            success: function(e) {
                t.util.showToast(e.data.message), 1 == e.data.result && (a.postNotificationName("NeedUpdateOrderAskList"), 
                setTimeout(function() {
                    wx.navigateBack({});
                }, 1200));
            },
            fail: function() {
                t.util.showToast("删除失败");
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
    delUser: function(a, e) {
        var i = this;
        t.network.post({
            url: t.url.RemoveWeddingOrderUser,
            params: {
                weddingOrderUserId: e
            },
            before: function() {
                wx.showLoading({
                    title: "正在删除"
                });
            },
            success: function(e) {
                if (wx.hideLoading(), t.util.showToast(e.data.message), 1 == e.data.result) {
                    var o = i.data.userData;
                    o.splice(a, 1), i.setData({
                        userData: o
                    });
                }
            },
            fail: function() {
                wx.hideLoading(), t.util.showToast("删除失败");
            },
            complete: function() {}
        });
    },
    collectUser: function(a, e, i) {
        var o = this;
        t.network.post({
            url: 0 == i ? t.url.CollectWeddingOrderUser : t.url.UncollectWeddingOrderUser,
            params: {
                weddingOrderUserId: e
            },
            before: function() {
                wx.showLoading({
                    title: 0 == i ? "正在收藏" : "正在取消收藏"
                });
            },
            success: function(e) {
                if (t.util.showToast(e.data.message), 1 == e.data.result) {
                    var s = o.data.userData;
                    if (s[a].collect = 0 == i ? 1 : 0, o.setData({
                        userData: s
                    }), 1 == o.data.tabIndex) {
                        for (var r = s[a].weddingOrderUserId, n = o.data.newUserData, d = 0; d < n.length; d++) if (n[d].weddingOrderUserId == r) {
                            n[d].collect = 0 == i ? 1 : 0;
                            break;
                        }
                        o.setData({
                            newUserData: n
                        });
                    }
                }
            },
            fail: function() {
                t.util.showToast("操作失败");
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
    cooperateUser: function(a, e, i) {
        var o = this;
        t.network.post({
            url: 0 == i ? t.url.AddCooperationUser : t.url.CancelCooperationUser,
            params: {
                userAccessToken: e,
                marketPrice: "",
                cooperationPrice: ""
            },
            before: function() {
                wx.showLoading({
                    title: 0 == i ? "正在添加合作" : "正在取消合作"
                });
            },
            success: function(e) {
                if (t.util.showToast(e.data.message), 1 == e.data.result) {
                    var s = o.data.userData;
                    s[a].cooperation = 0 == i ? 1 : 0, o.setData({
                        userData: s
                    });
                }
            },
            fail: function() {
                t.util.showToast("操作失败");
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
    loadUserData: function(a) {
        if (!this.data.isLoading && this.data.newUserData.length != this.data.newUserTotal) {
            this.data.isLoading = !0;
            var e;
            e = a ? 1 : this.data.newUserPageNo;
            var i = this;
            t.network.post({
                url: t.url.ListWeddingOrderUser,
                params: {
                    weddingOrderId: i.data.weddingOrderId,
                    type: 1,
                    pageNo: e,
                    pageSize: 20,
                    lastLookTime: i.data.detail.lastLookTime
                },
                before: function() {
                    wx.showNavigationBarLoading();
                },
                success: function(e) {
                    if (1 == e.data.result) {
                        var o = e.data.data.weddingOrderUsers, s = e.data.data.total;
                        0 == a && (o = i.data.newUserData.concat(o)), i.setData({
                            newUserTotal: s,
                            newUserData: o,
                            userData: o,
                            newUserPageNo: i.data.newUserPageNo + 1
                        });
                    } else t.util.showToast(e.data.message);
                },
                fail: function() {
                    t.util.showToast("获取数据失败");
                },
                complete: function() {
                    wx.hideNavigationBarLoading(), i.data.isLoading = !1;
                }
            });
        }
    },
    loadCollectUserData: function(a) {
        if (!this.data.isLoading && this.data.userData.length != this.data.collectUserTotal) {
            this.data.isLoading = !0;
            var e = this;
            a ? 1 : this.data.pageNo, t.network.post({
                url: t.url.ListWeddingOrderUser,
                params: {
                    weddingOrderId: e.data.weddingOrderId,
                    type: 2,
                    pageNo: 1,
                    pageSize: 20,
                    lastLookTime: e.data.detail.lastLookTime
                },
                before: function() {
                    wx.showNavigationBarLoading();
                },
                success: function(i) {
                    if (1 == i.data.result) {
                        var o = i.data.data.total, s = i.data.data.weddingOrderUsers;
                        0 == a && (s = e.data.userData.concat(s)), e.setData({
                            collectUserTotal: o,
                            userData: s,
                            pageNo: e.data.pageNo + 1
                        });
                    } else t.util.showToast(i.data.message);
                },
                fail: function() {
                    t.util.showToast("获取数据失败");
                },
                complete: function() {
                    wx.hideNavigationBarLoading(), e.data.isLoading = !1;
                }
            });
        }
    },
    updateLocalDataSeletUser: function(t) {
        for (var e = this.data.userData, i = 0; i < e.length; i++) {
            var o = e[i];
            o.weddingOrderUserId == t ? o.select = 1 : o.select = 2;
        }
        var s = this.data.detail;
        s.state = 2, this.setData({
            userData: e,
            detail: s
        }), a.postNotificationName("NeedUpdateOrderAskList");
    },
    updateLocalDataStop: function(t) {
        var a = this.data.detail;
        a.state = 3, this.setData({
            detail: a
        });
    },
    onLoad: function(t) {
        this.data.weddingOrderId = t.weddingOrderId, this.loadData();
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {
        0 == this.data.tabIndex ? this.loadUserData(!1) : this.loadCollectUserData(!1);
    },
    onShareAppMessage: function(t) {
        var a = t.target;
        return void 0 != a && (a = t.target.dataset.detail), void 0 != a ? (a = t.target.dataset.detail, 
        {
            title: a.shareTitle,
            path: a.shareUrl,
            imageUrl: a.shareImage,
            success: function(t) {},
            fail: function(t) {}
        }) : {
            title: this.data.detail.weixinProgramShareInfo.shareTitle,
            path: this.data.detail.weixinProgramShareInfo.shareUrl,
            imageUrl: this.data.detail.weixinProgramShareInfo.shareImage,
            success: function(t) {},
            fail: function(t) {}
        };
    }
});